/**
 * 数字转为汉字
 */
export function convertToChinaNum(num) {
  const arr1 = ["零", "一", "二", "三", "四", "五", "六", "七", "八", "九"];
  const arr2 = [
    "",
    "十",
    "百",
    "千",
    "万",
    "十",
    "百",
    "千",
    "亿",
    "十",
    "百",
    "千",
    "万",
    "十",
    "百",
    "千",
    "亿"
  ]; // 可继续追加更高位转换值
  if (!num || isNaN(num)) {
    return "零";
  }
  const english = num.toString().split("");
  let result = "";
  for (let i = 0; i < english.length; i++) {
    const des_i = english.length - 1 - i; // 倒序排列设值
    result = arr2[i] + result;
    const arr1_index = english[des_i];
    result = arr1[arr1_index] + result;
  }
  // 将【零千、零百】换成【零】 【十零】换成【十】
  result = result.replace(/零(千|百|十)/g, "零").replace(/十零/g, "十");
  // 合并中间多个零为一个零
  result = result.replace(/零+/g, "零");
  // 将【零亿】换成【亿】【零万】换成【万】
  result = result.replace(/零亿/g, "亿").replace(/零万/g, "万");
  // 将【亿万】换成【亿】
  result = result.replace(/亿万/g, "亿");
  // 移除末尾的零
  result = result.replace(/零+$/, "");
  // 将【零一十】换成【零十】
  // result = result.replace(/零一十/g, '零十');//貌似正规读法是零一十
  // 将【一十】换成【十】
  result = result.replace(/^一十/g, "十");
  return result;
}

export function formatToWan(num) {
  // 将数字转换为字符串并去掉小数部分（如果需要保留小数部分，可以调整这里）
  const numStr = num.toString().split('.');
  const integerPart = numStr[0];
  const decimalPart = numStr[1] ? numStr[1].slice(0, 2) : ''; // 保留两位小数

  // 将整数部分转换为以万为单位的数字
  const wanUnit = parseInt(integerPart) / 10000;

  // 格式化结果字符串
  let result = wanUnit.toFixed(2); // 保留两位小数
  result += '万';

  // 如果存在小数部分，则添加小数部分
  if (decimalPart) {
    result += '.' + decimalPart;
  }

  return result;
}

export function debounce(fn , time) {
  let timer
  return function (...argu) {
    if (timer) {
      clearTimeout(timer)
      timer = null
    }
    timer = setTimeout(() => {
      fn(...argu)
      clearTimeout(timer)
      timer = null
    }, time)
  }
}

export const splitArr = (arr, num) => {
  let newArr = []
  for (let i = 0; i < arr.length;) {
    newArr.push(arr.slice(i, i += num));
  }
  return newArr
}

export const musicLevel = (level) => {
  let obj = {}
  switch (level) {
    case 'standard':
      obj = {
        status: 0,
        name: '标准'
      }
      break
    case 'exhigh':
      obj = {
        status: 1,
        name: '超清'
      }
      break
    case 'lossless':
      obj = {
        status: 2,
        name: '超清臻音'
      }
      break
    case 'hires':
      obj = {
        status: 3,
        name: 'Hi-Res'
      }
      break
    case 'jyeffect':
      obj = {
        status: 4,
        name: '环绕音'
      }
      break
    case 'sky':
      obj = {
        status: 5,
        name: '沉浸声'
      }
      break
    case 'jymaster':
      obj = {
        status: 6,
        name: '超清母带'
      }
      break
  }
  return obj
}

export const formattedTime = (duration) => {
  var seconds = Math.floor((duration / 1000) % 60),
    minutes = Math.floor((duration / (1000 * 60)) % 60);

  minutes = (minutes < 10) ? "0" + minutes : minutes;
  seconds = (seconds < 10) ? "0" + seconds : seconds;

  return minutes + ":" + seconds;
}

export const formattedTimes = (time) => {
  const minutes = Math.floor(time / 60);
  const seconds = Math.floor(time % 60);
  return `${minutes<10?'0'+minutes:minutes}:${seconds < 10 ? '0' : ''}${seconds}`;
}

export const resetArr = (arr , brr) => {
  let list = []
  for (let i = 0; i < arr.length; i++) {
    const item = arr[i];
    const index = brr.findIndex(song => song.id === item.id);
    if(index === -1) {
      list.push(item)
    }
  }
  return list;
}

export function shuffle(arr) {
  var random
  var newArr = []

  while (arr.length) {
    random = Math.floor(Math.random() * arr.length)
    newArr.push(arr[random])
    arr.splice(random, 1)
  }

  return newArr
}
